//
//  KZHorizonalViewController.m
//  开源中国
//
//  Created by kun on 15/10/8.
//  Copyright (c) 2015年 kun. All rights reserved.
//

#import "KZHorizonalViewController.h"
#import "Masonry.h"
#import "UIViewAdditions.h"

@interface KZHorizonalViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
//@property (nonatomic,copy)NSArray *controllers;

@end

@implementation KZHorizonalViewController

static NSString * const reuseIdentifier = @"Cell";


-(void)setControllers:(NSArray *)controllers
{
    _controllers = controllers;
    for (UIViewController *controller in controllers) {
        
        [self addChildViewController:controller];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
     [self createCollection];

    
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    // Do any additional setup after loading the view.
}

-(void)createCollection
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    //列间距
    layout.minimumInteritemSpacing = 0;//默认10
    //行间距
    layout.minimumLineSpacing = 0;//默认10
    
    //cell尺寸
    
    CGSize sizeScreen = [UIScreen mainScreen].bounds.size;
    
    layout.itemSize = CGSizeMake(sizeScreen.width ,sizeScreen.height - 64 ) ;//self.view.frame.size ;//默认50
    
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    
    UICollectionView *collection = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];

    //collection.collectionViewLayout = layout;
    [self.view addSubview:collection];
    _collectionView = collection;
    //_collectionView.frame = CGRectMake(0, 0, 375, 518);
    
    //创建约束
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(0));
        make.left.equalTo(@(0));
        make.right.equalTo(@(0));
        make.bottom.equalTo(@(0));
    }];
    
    
    [collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    collection.delegate = self;
    collection.dataSource = self;
    collection.backgroundColor = [UIColor grayColor];
    collection.pagingEnabled = YES;
    collection.showsHorizontalScrollIndicator = NO;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
 //   PKZLog(@"11");
#warning Incomplete method implementation -- Return the number of sections
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    
   // PKZLog(@"");
#warning Incomplete method implementation -- Return the number of items in the section
    return _controllers.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    // Configure the cell
    UIViewController *controller = _controllers[indexPath.row];
    controller.view.frame = cell.contentView.bounds;
    [cell.contentView addSubview:controller.view];
    
    return cell;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
  //  PKZLog(@"--->%@",NSStringFromCGPoint(scrollView.contentOffset));
    
    if(scrollView.contentOffset.x == 0)
    {
       // if(scrollView.contentOffset.x < 0)
       // {
          //  scrollView.userInteractionEnabled = NO;
       // }
    }

}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat W = self.collectionView.frame.size.width;
    NSInteger index = self.collectionView.contentOffset.x/W;
    if(_horizonalViewChang)
    {
        self.horizonalViewChang(scrollView,index);
    }

}



//- (void)willMoveToSuperview:(UIView *)newSuperview
//{
//
//}



//添加到


#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
